package net.caixiaomi.info.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.MatchLineUpAlertNateModel;
import net.caixiaomi.info.model.MatchLineupModel;
import net.caixiaomi.info.ui.view.MatchLineDrawView;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * Created by suntian on 2018/7/27.
 */

public class MatchLineupAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MatchLineupAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_VIEW_MATCH_LINEUP, R.layout.match_lineup_layout);
        addItemType(Constants.ITEM_VIEW_MATCH_LINEUP1, R.layout.match_lineup_bottom_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_VIEW_MATCH_LINEUP:
                    bindHeader(helper, item);
                    break;
                case Constants.ITEM_VIEW_MATCH_LINEUP1:
                    bindFooter(helper, item);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindHeader(BaseViewHolder helper, MultiItemEntity item){

        LinearLayout group=helper.getView(R.id.match_lineup_group);
        int mWidth= CommonApp.mScreenWidth;
        int mHeight=1364*mWidth/750;
        RecyclerView.LayoutParams params= (RecyclerView.LayoutParams) group.getLayoutParams();
        params.width=mWidth;
        params.height=mHeight;
        group.setLayoutParams(params);
        MatchLineDrawView lDrawView=helper.getView(R.id.match_lineup_in_view);
        lDrawView.setData((MatchLineupModel) item,mWidth,mHeight);

    }

    private void bindFooter(BaseViewHolder helper, MultiItemEntity item){

        MatchLineUpAlertNateModel model= (MatchLineUpAlertNateModel) item;

        if(model.isNoData()){
            helper.setGone(R.id.bottom_person_layout,false);
            helper.setGone(R.id.lineup_text_empty,true);

        }else{
            helper.setGone(R.id.bottom_person_layout,true);
            helper.setGone(R.id.lineup_text_empty,false);
            RelativeLayout lLeftLayout=helper.getView(R.id.line_person_left);
            if(model.getmHomePerson()!=null){
                lLeftLayout.setVisibility(View.VISIBLE);
                helper.setText(R.id.lineup_home_person_name,model.getmHomePerson().getPersonName());
                helper.setText(R.id.bottom_home_num_tv,model.getmHomePerson().getShirtNumber());
            }else{
                lLeftLayout.setVisibility(View.INVISIBLE);
            }

            RelativeLayout lRightLayout=helper.getView(R.id.line_person_right);
            if(model.getmVisitorPerson()!=null){
                lRightLayout.setVisibility(View.VISIBLE);
                helper.setText(R.id.lineup_visitor_person_name,model.getmVisitorPerson().getPersonName());
                helper.setText(R.id.bottom_visitor_num_tv,model.getmVisitorPerson().getShirtNumber());
            }else{
                lRightLayout.setVisibility(View.INVISIBLE);
            }

        }

        helper.setGone(R.id.lineup_header,model.isFirst());
        helper.setGone(R.id.lineup_header_line,model.isFirst());

        helper.setText(R.id.title,model.isInjureies()?mContext.getResources().getString(R.string.C_INJURIES):mContext.getResources().getString(R.string.C_BENCH));
        helper.setText(R.id.lineup_home_team_name,model.getHomeTeamAbbr());
        helper.setText(R.id.lineup_visitor_team_name,model.getVisitingTeamAbbr());


    }
}
